//
//  RecommendView.swift
//  FM
//
//  Created by x on 2017/8/23.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol RecommendViewDelegate:NSObjectProtocol {
    
    /// 点击跳转相应的Web页面
    ///
    /// - Parameter webURl: web页面url
    func btuWillClick(index: Int)
}

class RecommendView: UIView {
    
    weak var delegate:RecommendViewDelegate?
    
    /// 数据源
    var models:[URL]? {
        didSet {
            guard models != nil else {
                return
            }
            let w = bounds.width
            let h = bounds.height
            //1.设置scrollView的滚动区域
            scrollView.contentSize = CGSize(width: w*CGFloat(models!.count), height: 0)
            //2.创建显示的图片
            for i in 0..<models!.count {
                let btu = UIButton()
                btu.frame = CGRect.init(x: CGFloat(i)*w, y: 0, width: w, height: h)
                
                SDWebImageManager.shared().loadImage(with: models![i], options: SDWebImageOptions.init(rawValue: 0), progress: nil, completed: { (image, _, error, _, finish, url) in
                    btu.setBackgroundImage(image, for: UIControlState.normal)
                })
//                SDWebImageManager.shared().downloadImage(with: models![i], options: SDWebImageOptions.init(rawValue: 0), progress: nil, completed: { (image, error, _, finish, url) in
//                    btu.setBackgroundImage(image, for: UIControlState.normal)
//                })
                
                btu.tag = i
                btu.addTarget(self, action: #selector(btuClick(btu:)), for: UIControlEvents.touchUpInside)
                scrollView.addSubview(btu)
            }
            
            //3.创建小圆点
            pageController.numberOfPages = models!.count
            addSubview(pageController)
            
            //4添加一个定时器
            Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(timerChange), userInfo: nil, repeats: true)
        }
    }
    
    /// 按钮点击事件
    ///
    /// - Parameter btu: <#btu description#>
    func btuClick(btu:UIButton) -> Void {
        
        delegate?.btuWillClick(index: btu.tag)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //1.添加子控件
        addSubview(scrollView)
    }
    
    func pageControllerClick(sender:UIPageControl) -> Void {
        scrollView.contentOffset = CGPoint(x: CGFloat(sender.currentPage)*bounds.width, y: 0)
    }
    
    func timerChange() -> Void {
        
        if pageController.currentPage == models!.count-1 {
            pageController.currentPage = 0
        }else {
            pageController.currentPage += 1
        }
        UIView.animate(withDuration: 0.5) {
            self.scrollView.contentOffset = CGPoint(x: CGFloat(self.pageController.currentPage)*self.bounds.width, y: 0)
        }
    }
    //MARK: -- 懒加载
    /// ScrollView
    private lazy var scrollView:UIScrollView = {
        let tt = UIScrollView()
        tt.frame = self.bounds
        tt.isPagingEnabled = true
        tt.showsVerticalScrollIndicator = false
        tt.showsHorizontalScrollIndicator = false
        tt.delegate = self
        
        return tt
    }()
    
    /// 圆点
    fileprivate lazy var pageController:UIPageControl = {
        let page = UIPageControl()
        page.frame = CGRect.init(x: 0, y: self.bounds.height-30, width: self.bounds.width, height: 20)
        page.currentPage = 0
        page.pageIndicatorTintColor = UIColor.white
        page.currentPageIndicatorTintColor = UIColor.red
        page.addTarget(self, action: #selector(pageControllerClick(sender:)), for: UIControlEvents.valueChanged)
        
        return page
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension RecommendView:UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offSetX = scrollView.contentOffset.x
        pageController.currentPage = Int(offSetX/bounds.width)
    }
}
